<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 17-4-27
 * Time: 上午9:21
 */

namespace Admin\Model;

use Think\Model\RelationModel;

class GoodsModel extends RelationModel{




    protected $_validate =array(
        array('gname','require','商品名不能为空'),
        array('content','require','内容名不能为空'),
        array('number','require','库存量不能为空',1),
        array('desc','require','描述不能为空'),
    );


    protected $_link = array(
        'Profile'=>array(
            'mapping_type'  => self::BELONGS_TO,
            'class_name'    => 'Prodcates',
            'foreign_key'   => 'gcats',
            'mapping_name'  => 'cat_info',
        ),

        'Prozfile'=>array(
            'mapping_type'  => self::BELONGS_TO,
            'class_name'    => 'Brand',
            'foreign_key'   => 'brand_id',
            'mapping_name'  => 'b_info',
        ),

    );




    protected $_auto = array (
        array('addtime','time',1,'function'),
    );

    //    插入之前钩子，保存图片用
    protected  function _before_insert(&$data,$option) {
        if(isset($_FILES['image']) && $_FILES['image']['tmp_name'] ){
            $upload = new \Think\Upload();
            $upload->maxSize   =     3145728 ;// 1M
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
            $upload->savePath  =     'Picture/';    // 设置附件上传（子）目录
            $info   =   $upload->upload();
            $logo = '/Uploads/'.$info['image']['savepath'] . $info['image']['savename'];
            $data['image'] = $logo;
        }

        // 调用添加之前钩子，为货号拼接个数字存进去
      //  $data['goods_no']=time() . rand(11111,99999);
    }
    //    编辑之前钩子，保存图片用
    protected function _before_update(&$data, $option)
    {
        $oldlogo = I('post.oldlogo');
        if($oldlogo)
        @unlink($oldlogo);
        //p($oldlogo);die;
       // if($oldlogo)
        if(!empty($_FILES['image']['tmp_name'])){
            $upload = new \Think\Upload();
            $upload->maxSize   =     3145728 ;// 3M
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
            $upload->savePath  =     'Picture/';    // 设置附件上传（子）目录
            $info   =   $upload->upload();
            $logo = '/Uploads/'.$info['image']['savepath'] . $info['image']['savename'];
            $data['image'] = $logo;
        }
        else{
            $data['image']=$oldlogo;
        }
    }


    public function search()
    {
        $where=1;
        if($search = I('get.search')){
            $where .= ' AND gname OR content LIKE "%'.$search.'%"';}
        $perpage = 5;
        // 获取总的记录数
        $totalRecord = $this->where($where)->count();
        // 生成翻页的对象
        $page = new \Think\Page($totalRecord, $perpage);
        $page->setConfig('next','下一页');
        $page->setConfig('prev','上一页');
        return array(
            'data' => $this->limit($page->firstRow, $page->listRows)->
                    where($where)->
                    order('id desc')->relation(true)->select(),
            'page' => $page->show(), // 翻页的字符串
        );
    }


} 